package com.fosu.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fosu.common.ResponseResult;
import com.fosu.dao.CampusDao;
import com.fosu.dao.UserDao;
import com.fosu.domain.User;
import com.fosu.service.UserService;
import com.fosu.vo.Reply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Desription
 * 用户业务实现类
 *
 * @author manno
 * @date 2020/10/19
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private CampusDao campusDao;

    @Override
    public ResponseResult findAll(HttpServletResponse resp) {
        List<User> userList = userDao.findAll();
        if (userList.size() > 0) {
            return ResponseResult.build(resp, 200, 10015, "获取用户列表成功", userList);
        } else {
            return ResponseResult.build(resp, 500, 10016, "获取用户列表失败", userList);
        }

    }

    @Override
    public ResponseResult findUserById(HttpServletResponse resp, int userId) {
        User user = userDao.findUserById(userId);
        if (user != null) {
            return ResponseResult.build(resp, 200, 10017, "根据id获取用户成功", user);
        }
        return ResponseResult.build(resp, 200, 10018, "根据id获取用户失败", user);
    }

    @Override
    @Transactional
    public ResponseResult editUserNickName(HttpServletResponse resp,JSONObject newNickName)
    {
        try{
            String nickName = newNickName.getString("nickName");
            Integer id = newNickName.getInteger("userId");
            userDao.editUserNickName(id, nickName);
        } catch (ClassCastException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.build(resp,500,30017,"修改用户名失败",null);
        }
        return ResponseResult.build(resp, 200, 30016, "修改用户名成功", null);
    }

    @Override
    public ResponseResult editUserCampus(HttpServletResponse resp, JSONObject newCampus) {
        try {
            Integer campusId = newCampus.getInteger("newCampusId");
            Integer id = newCampus.getInteger("userId");
            /**如果用户暂无校区记录*/
            if (campusDao.findCampusByUserId(id) == null) {
                /**就增加一条记录*/
                userDao.addUserCampus(id, campusId);
            } else {
                /**否则修改原有记录*/
                userDao.editUserCampus(id, campusId);
            }

        } catch (ClassCastException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.build(resp,500,30019,"修改用户校区失败",null);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return ResponseResult.build(resp, 200, 30018, "修改用户校区成功", null);
    }

    @Override
    @Transactional
    public ResponseResult editUserPhone(HttpServletResponse resp, JSONObject newPhone)
    {
        try{
            String phone = newPhone.getString("phone");
            Integer id = newPhone.getInteger("userId");
            userDao.editUserPhone(id, phone);
        } catch (ClassCastException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.build(resp,500,30021,"修改用户手机号失败",null);
        }
        return ResponseResult.build(resp, 200, 30020, "修改用户名手机号成功", null);
    }

    @Override
    @Transactional
    public ResponseResult editUserWechat(HttpServletResponse resp, JSONObject newWechat) {
        String wxRegex = "^[a-zA-Z][a-zA-Z\\d_-]{5,19}$";
        String wechat = null;
        try {
            wechat = newWechat.getString("wechat");
            Integer id = newWechat.getInteger("userId");
//
//            if (!wechat.matches(wxRegex)) {
//                return ResponseResult.fail(resp, "微信号格式不正确(＾Ｕ＾)ノ~ＹＯ");
//            }

            userDao.editUserWechat(id, wechat);
        } catch (ClassCastException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.build(resp,500,30023,"修改用户微信号失败",null);
        }
        return ResponseResult.build(resp, 200, 30022, "修改用户名微信号成功", null);
    }

    @Override
    public ResponseResult iReleased(HttpServletResponse resp, Integer userId) {
        try {
            Integer i = userDao.iReleased(userId);
            if (i == null) {
                return ResponseResult.build(resp, 500, 10021, "按用户id获取用户发布商品数失败", null);
            } else {
                return ResponseResult.build(resp, 200, 10020, "按用户id获取用户发布商品数成功", i);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.build(resp, 500, 10021, "按用户id获取用户发布商品数失败", null);
        }
    }

    @Override
    @Transactional
    public ResponseResult getReplyMe(HttpServletResponse resp, Integer userId)
    {
        try{
            /**获取上次点击回复我的时间*/
            userDao.getLastCommentTime(userId);
            /**获取回复我的具体评论信息*/
            List<Reply> replies = userDao.getReplyMe(userId);
            /**保存这次点击回复我的时间*/
            userDao.saveLastCommentTime(userId,new Date());
            return ResponseResult.build(resp, 200, 10028, "按用户id获取他人回复用户信息集合成功", replies);
        }catch (RuntimeException e)
        {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        return ResponseResult.build(resp, 500, 10029, "按用户id获取他人回复用户信息集合失败", null);
    }
}
